#! /usr/bin/python

import os
import sys

data = 0 

def setUpData(sql, path):

    global data

    if sql  in ["q1_1","q1_2","q1_3"]:
        cmd = "cp " + path + "/customer/LINEORDER* ."
        if data !=3:
            os.system(cmd)
            print cmd
            data = 3 

    if sql  in ["q2_1","q2_2", "q2_3"]:

        cmd = "cp " + path + "/part/LINEORDER* ."
        if data != 2:
            os.system(cmd)
            print cmd
            data = 2 

    elif sql in ["q3_1", "q3_2", "q3_3", "q3_4"]:
        cmd = "cp " + path + "/customer/LINEORDER* . "
        if data != 3:
            os.system(cmd)
            data =3

    elif sql  in ["q4_1", "q4_3"]:
        cmd = "cp " + path + "/supplier/LINEORDER* ."
        if data !=4:
            os.system(cmd)
            data = 4

    elif sql  in ["q4_2"]:
        cmd = "cp " + path + "/customer/LINEORDER* . "
        if data != 3:
            os.system(cmd)
            data = 3


cmd = ""
queryPath="./test/ssb_test/" # relative path to gpuDBPath
schemaPath = "./test/ssb_test/ssb.schema"
srcPath = "src/opencl/"
resultPath = "./result/" #relative path compared to srcPath
dataPath = "./data/gpu/"

translator = "./translate.py"

files = os.listdir(queryPath)
files.sort()

pwd = os.getcwd()

cmd="unalias cp"
os.system(cmd)

for file in files:
    nameA = file.split(".sql")
    if len(nameA) != 2:
        continue

    cmd = translator + " " + queryPath + file + " " + schemaPath
    os.system(cmd)

    os.chdir(srcPath)

    if os.path.exists(resultPath) is False:
        os.makedirs(resultPath)

    setUpData(nameA[0], dataPath)

    cmd = "make all"
    os.system(cmd)

    cmd = "sleep 1"
    os.system(cmd)

    cmd = "echo 1 > /proc/sys/vm/drop_caches"
    #os.system(cmd)

    cmd = "./GPUDATABASE > " + resultPath +"/" + nameA[0]
    os.system(cmd)
    os.system(cmd)

    os.chdir(pwd)

os.chdir(srcPath)
files = os.listdir(resultPath)
files.sort()

print "Total time"
for file in files:
    fpath = resultPath + "/" + file
    fd = open(fpath, "r")
    lines = fd.readlines()
    for line in lines:
        t = line.rstrip().split("Total Time:")
        if len(t) != 2:
            continue
        print t[1].strip()

    fd.close()

print "Disk time"
for file in files:
    fpath = resultPath + "/" + file
    fd = open(fpath, "r")
    lines = fd.readlines()
    for line in lines:
        t = line.rstrip().split("Disk Load Time:")
        if len(t) != 2:
            continue
        print t[1].strip()

    fd.close()

print "PCIe Time:"
for file in files:
    fpath = resultPath + "/" + file
    fd = open(fpath, "r")
    lines = fd.readlines()
    for line in lines:
        t = line.rstrip().split("PCIe Time:")
        if len(t) != 2:
            continue
        print t[1].strip()

    fd.close()

print "kernel Time:"
for file in files:
    fpath = resultPath + "/" + file
    fd = open(fpath, "r")
    lines = fd.readlines()
    for line in lines:
        t = line.rstrip().split("Kernel Time:")
        if len(t) != 2:
            continue
        print t[1].strip()

    fd.close()

